﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TF.Access;
using System.IO;
using System.Drawing;

public partial class CreateTrip : System.Web.UI.Page
{
    private string SCRIPT_TEMPLATE = "<script>alert('{0}')</script>";
    protected string fromCityName;
    
    //protected string toTarget;
    //protected string ehId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fromCityName = "太原市";
        }
        else
        {
            string ehIdStr = this.txtEhId.Value;
            if (!string.IsNullOrEmpty(ehIdStr))
            {
                try
                {
                    int ehId = int.Parse(ehIdStr);
                    TF_EventH eh = TF_EventH.Select(ehId);
                    string picUrl = GetImageURL(fuPicURL);
                    if (!string.IsNullOrEmpty(picUrl))
                    {
                        eh.PicUrl = picUrl;
                        eh.EventTitle = Request["txtEventTitle"];
                        eh.Memo = Request["txtEventMemo"];
                        eh.Update();
                        Response.Redirect(string.Format("TripPlan.aspx?ehId={0}", ehId));
                    }
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(typeof(CreateTrip), "errorAlert", string.Format(SCRIPT_TEMPLATE, "有错误发生,创建失败" + ex.Message));
                }
            }
        }
    }

    private string GetImageURL(HtmlInputFile filPhoto)
    {
        string fileSrc = ConfigurationManager.AppSettings["MediaURL"] + "/" + "empty.jpg";

        if (HttpContext.Current.Request.Files.Count > 0)
        {
            if (filPhoto.PostedFile.ContentLength > int.Parse(ConfigurationManager.AppSettings["ImageSize"]))
            {
                this.ClientScript.RegisterStartupScript(typeof(CreateTrip), "Overbigsize", string.Format(SCRIPT_TEMPLATE, "上传文件超过4M,使用默认图片!"));
            }
            else if (!IsValidImageFile(filPhoto))
            {
                this.ClientScript.RegisterStartupScript(typeof(CreateTrip), "InvalidFormat", string.Format(SCRIPT_TEMPLATE, "上传文件不是有效的图片文件"));
            }
            else
            {
                TF_UserProfile userProfile = WebUtility.GetUserProfile();
                if (userProfile != null)
                {
                    try
                    {
                        string fileName = Path.GetFileName(filPhoto.PostedFile.FileName);
                        string uploadPath = Server.MapPath(ConfigurationManager.AppSettings["MediaURL"]) + Path.DirectorySeparatorChar + fileName;
                        fileSrc = ConfigurationManager.AppSettings["MediaURL"] + "/" + fileName;
                        filPhoto.PostedFile.SaveAs(uploadPath);

                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {

                }
            }
        }

        return fileSrc;
    }

    private bool IsValidImageFile(HtmlInputFile file)
    {
        if (file.PostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
        {
            return false;
        }

        bool ret = false;
        try
        {
            using (Bitmap bmp = new Bitmap(file.PostedFile.InputStream))
            {
                ret = true;
            }
        }
        catch (ArgumentException)
        {
            //throws exception if not valid image
            ret = false;
        }

        return ret;


    }
}
